<?php
/**

 * Date: 2015/7/24

 */
session_start();
class BaseAction extends Action{
    protected $info=array();
    protected $webname;
    protected $nologin=array();
    function _initialize(){
        //$this->info=mangerlogin::account();
        if(!in_array(Req::request('a'),$this->nologin ))return $this->login_check();
        //$this->assign('info',$this->info);
        //$this->webname='工作小助手';
        $this->assign('c',Req::get('c'));
    }
    protected function log($uid,$type,$con){
        db::_insert('log',array('masterid'=>$this->info['masterid'],'bmasterid'=>$uid,'createdate'=>date('Y-m-d H:i:s'),'ip'=>get_ip(),'con'=>$con));
    }
    function result($status=0,$msg=''){
        echo json_encode(['status'=>$status,'message'=>$msg],JSON_UNESCAPED_UNICODE);
    }
    function login_check(){
        if (empty($_COOKIE["usersid"]) || empty($_COOKIE["password"]) || empty($_COOKIE["username"])){
            session_destroy();

            echo "<script>location.href='/login.php';</script>";
            exit;
        }else{
            if(!isset($_SESSION['usersid']))
            {
                $ip = get_ip();
                $username = Req::request($_COOKIE["username"]);

                $pwd = $_COOKIE["password"];
                $pwd = (strlen($pwd) > 30) ? substr($pwd,0,30) : $pwd;
                $pwd = $this->setPassword($pwd);

                $username = (strlen($username) > 50) ? substr($username,0,50) : $username;


                $sql = "call web_user_login('{$username}','{$pwd}','{$ip}')";
                $arr = db::get_all($sql,'assoc');
              switch($arr[0][0]["result"])
                {
                    case '0': //成功
                        $_SESSION["usersid"] = $arr[0][0]["userid"];
                        $_SESSION["username"] = $arr[0][0]["username"];
                        $_SESSION["password"] = $pwd;
                        $_SESSION["nickname"] = $arr[0][0]["nickname"];
                        $_SESSION["points"] = $arr[0][0]["points"];
                        $_SESSION["bankpoints"] = $arr[0][0]["bankpoint"];
                        $_SESSION["exp"] = $arr[0][0]["experience"];
                        $_SESSION['freeze'] = 0;
                        setcookie("usersid",$arr[0][0]["userid"]);
                        setcookie("username",$arr[0][0]["username"]);
                        setcookie("password",$pwd);
                    case '2': //帐号被冻结
                        setcookie("usersid");
                        setcookie("username");
                        setcookie("password");
                        echo "<script language=javascript>alert('很抱歉!您帐号被冻结无法登录，请与客服联系!');location.href='/';</script>";
                        exit;
                    case '99': //数据库错误
                        echo "<script language=javascript>alert('很抱歉!由于系统故障暂时无法登录，请与客服联系!');location.href='/';</script>";
                        exit;
                    default:
                        echo "<script language=javascript>location.href='/login.php';</script>";
                        exit;
                }
            }
            //如果帐号被冻结，立即注销退出
            if(isset($_SESSION['freeze']) && $_SESSION['freeze'] == 1)
            {
                //退出
                session_destroy();
                setcookie("usersid");
                setcookie("username");
                setcookie("password");
                echo "<script language=javascript>location.href='index.php';</script>";
                exit;
            }
        }
    }
    private function setPassword($str){
        return md5($GLOBALS['web_pwd_encrypt_prefix'].$str);
    }
    /* 更新分数
 *
*/
    function RefreshPoints()
    {
        $sql = "select points,back,dj from users where id = '{$_SESSION['usersid']}'";
        $list=db::get_all($sql,'assoc');
        foreach ($list as $rs)
        {
            $_SESSION['points'] = $rs['points'];
            $_SESSION['bankpoints'] = $rs['back'];
            $_SESSION['freeze'] = $rs['dj'];
        }
        //如果帐号被冻结，立即注销退出
        if(isset($_SESSION['freeze']) && $_SESSION['freeze'] == 1)
        {
            //退出
            session_destroy();
            setcookie("usersid");
            setcookie("username");
            setcookie("password");
            echo "<script language=javascript>location.href='index.php';</script>";
            exit;
        }
    }
    function update_centerbank($point,$type){
        //-- 更新中央银行
        $sql='UPDATE centerbank SET score = score - '.$point.' WHERE bankIdx = '.$type;
        db::_query($sql);
    }
}